package com.example.bbs.service.impl;

import com.example.bbs.domain.entity.Comment;
import com.example.bbs.domain.entity.Post;
import com.example.bbs.domain.entity.User;
import com.example.bbs.domain.model.ResponseResult;
import com.example.bbs.kafka.producer.CommentProducer;
import com.example.bbs.kafka.producer.PostProducer;
import com.example.bbs.service.CommentService;
import com.example.bbs.service.PostService;
import com.example.bbs.util.HostHold;
import com.example.bbs.util.RedisKeyUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@Slf4j
public class LikeServiceImpl implements com.example.bbs.service.LikeService {

    @Resource
    private RedisTemplate postInfoRedisTemplate;

    @Resource
    private RedisTemplate commentInfoRedisTemplate;

    @Resource
    private RedisTemplate commonRedisTemplate;
    
    @Autowired
    private PostService postService;
    
    @Autowired
    private CommentService commentService;
    
    @Autowired
    private PostProducer postProducer;
    
    @Autowired
    private CommentProducer commentProducer;



    /**
     * 点赞帖子
     * @param postId
     */
    public ResponseResult doLikePost(String postId) {

        User user = HostHold.get();
        //判断点赞状态
        Boolean likeStatus = isLikePost(postId, user.getUserId());
        //如果没有点过赞
        if (!likeStatus) {
            //点赞
            String likePostSetKey = RedisKeyUtil.getLikePostSetKey(postId);

            commonRedisTemplate.opsForSet().add(likePostSetKey, user.getUserId());
            postInfoRedisTemplate.opsForHash().increment(postId, "likeCount", 1);

            //发送帖子信息更新消息
            Post post = postService.getPostByPostId(postId);
            postProducer.send("post-update", post);

            //存储热点帖子集合中
            postService.addChangedPostSet(postId);

            return ResponseResult.success("点赞成功!");
        }
        return ResponseResult.failure("已经点过赞了！");




    }

    /**
     * 取消点赞帖子
     * @param postId
     */
    public ResponseResult undoLikePost(String postId) {

        User user = HostHold.get();
        //判断点赞状态
        Boolean likeStatus = isLikePost(postId, user.getUserId());
        //点过赞
        if (likeStatus) {

        String likePostSetKey = RedisKeyUtil.getLikePostSetKey(postId);

        commonRedisTemplate.opsForSet().remove(likePostSetKey, user.getUserId());
        postInfoRedisTemplate.opsForHash().increment(postId, "likeCount", -1);

        //发送帖子信息更新消息
        Post post = postService.getPostByPostId(postId);
        postProducer.send("post-update", post);
            return ResponseResult.success("取消点赞成功!");
        }
        return ResponseResult.failure("你没有点过赞，无法取消点赞！");
    }

    /**
     * 点赞评论
     * @param commentId
     */
    public ResponseResult doLikeComment(String commentId) {

        User user = HostHold.get();

        //判断点赞状态
        Boolean likeStatus = isLikeComment(commentId, user.getUserId());
        //如果没有点过赞
        if (!likeStatus) {
            String likeCommentSetKey = RedisKeyUtil.getLikeCommentSetKey(commentId);

            commonRedisTemplate.opsForSet().add(likeCommentSetKey, user.getUserId());
            commentInfoRedisTemplate.opsForHash().increment(commentId, "likeCount", 1);

            //发送评论信息更新消息
            Comment comment = commentService.getCommentById(commentId);
            commentProducer.send("comment-update", comment);
            return ResponseResult.success("点赞成功!");
        }
        return ResponseResult.failure("已经点过赞了！");
    }

    /**
     * 取消点赞评论
     * @param commentId
     */
    public ResponseResult undoLikeComment(String commentId) {

        User user = HostHold.get();
        //判断点赞状态
        Boolean likeStatus = isLikeComment(commentId, user.getUserId());
        //点过赞
        if (likeStatus) {
            //取消点赞
            String likeCommentSetKey = RedisKeyUtil.getLikeCommentSetKey(commentId);

            commonRedisTemplate.opsForSet().remove(likeCommentSetKey, user.getUserId());
            commentInfoRedisTemplate.opsForHash().increment(commentId, "likeCount", -1);

            //发送评论信息更新消息
            Comment comment = commentService.getCommentById(commentId);
            commentProducer.send("comment-update", comment);
            return ResponseResult.success("取消点赞成功!");
        }
        return ResponseResult.failure("你没有点过赞，无法取消点赞！");



    }



    /**
     * 判断用户是否点赞该帖子
     * @param userId
     * @param postId
     * @return
     */
    public Boolean isLikePost(String postId, String userId) {
        String likePostSetKey = RedisKeyUtil.getLikePostSetKey(postId);
        return commonRedisTemplate.opsForSet().isMember(likePostSetKey, userId);
    }

    /**
     * 判断用户是否点赞该评论
     * @param userId
     * @param commentId
     * @return
     */
    public Boolean isLikeComment(String commentId, String userId) {
        String likeCommentSetKey = RedisKeyUtil.getLikeCommentSetKey(commentId);
        return commonRedisTemplate.opsForSet().isMember(likeCommentSetKey, userId);
    }

}
